package net.chrisjrock.display 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Christopher J. Rock
	 */
	public class HotButton extends MovieClip
	{
		protected var _buttonEnabled:Boolean;
		
		public function HotButton() 
		{
			stop();
			buttonEnabled = true;
		}
		
		protected function addListeners ():void
		{
			addEventListener(MouseEvent.ROLL_OUT , rollOutHandler , false , 0 , true);
			addEventListener(MouseEvent.ROLL_OVER , rollOverHandler , false , 0 , true);
			addEventListener(MouseEvent.MOUSE_DOWN , mouseDownHandler , false , 0 , true);
			addEventListener(MouseEvent.MOUSE_UP , mouseUpHandler , false , 0 , true);
		}
		
		protected function removeListeners ():void
		{
			removeEventListener(MouseEvent.ROLL_OUT , rollOutHandler);
			removeEventListener(MouseEvent.ROLL_OVER , rollOverHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN , mouseDownHandler);
			removeEventListener(MouseEvent.MOUSE_UP , mouseUpHandler);
		}
		
		protected function rollOutHandler (event:MouseEvent):void
		{
			if (totalFrames >= 1)
			{
				gotoAndStop(1);
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		protected function rollOverHandler (event:MouseEvent):void
		{
			if (totalFrames >= 2)
			{
				gotoAndStop(2);
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		protected function mouseDownHandler (event:MouseEvent):void
		{
			if (totalFrames >= 3)
			{
				gotoAndStop(3);
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		protected function mouseUpHandler (event:MouseEvent):void
		{
			if (totalFrames >= 2)
			{
				gotoAndStop(2);
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		public function get buttonEnabled ():Boolean
		{
			return _buttonEnabled;
		}
		
		public function set buttonEnabled (value:Boolean):void
		{
			if (_buttonEnabled != value)
			{
				_buttonEnabled = value;
				
				if (_buttonEnabled)
				{
					mouseEnabled = buttonMode = true;
					mouseChildren = false;
					addListeners();
				}
				else
				{
					removeListeners();
					mouseEnabled = buttonMode = false;
				}
			}
		}
		
	}

}